<template>
  <div class="player-box">
    <!-- <p>Current Playing</p> -->
    <div style="margin-left: 20px;display: flex; justify-content: center; align-items: center; height: 100px;">
      <el-image style="width: 80px; height: 80px; border-radius: 8px;" :src="calcImgUrl(albumid)"/>
      <el-icon :size="60" class="clickable"
        v-if="!playing" @click="changePlayingStatus"> 
        <VideoPlay />
      </el-icon>
      <el-icon :size="60" class="clickable" v-if="playing" @click="changePlayingStatus">
        <VideoPause />
      </el-icon>
      <div class="description-container">
        <div>{{ songname }}</div>
        <div>{{ singer }} - {{ albumname }}</div>
    </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'player',
  props: {
    // currentAlbumId: { type: String },
    // currentSong: { type: String },
    // currentSinger: { type: String },
    // currentAlbumName: { type: String },
    playingSongId: { type: Number }
  },
  data() {
    return {
      playing: true,
      songid: -1,
      albumid: -1,
      songname: '',
      singer: '',
      albumname: '',
    }
  },
  methods: {
    changePlayingStatus() {
      this.playing = !this.playing
    },
    calcImgUrl(id) {
      // return require("src/assets/albums/" + id + ".jpeg")
      return new URL("../assets/albums/" + id + ".jpeg", import.meta.url).href
    },
    getSongInfo(id) {
      axios({
      method: 'post',
      url: '/api/getDetailsBySongId',
      params: {
        songid: id
      }
    }).then((res) => {
      if (res.data.success) {
        this.albumid = res.data.data[0].albumid
        this.songname = res.data.data[0].name
        this.singer = res.data.data[0].singername
        this.albumname = res.data.data[0].albumname
      }
    })
    }
  },
  mounted() {
    console.log(this.playingSongId)
    axios({
      method: 'post',
      url: '/api/getDetailsBySongId',
      params: {
        songid: this.playingSongId
      }
    }).then((res) => {
      if (res.data.success) {
        this.$nextTick(() => {
          this.albumid = res.data.data[0].albumid
          this.songname = res.data.data[0].name
          this.singer = res.data.data[0].singername
          this.albumname = res.data.data[0].albumname
        })
      }
    })
  },
  watch: {
    playingSongId() {
      this.songid = this.playingSongId
      this.getSongInfo(this.songid)
    }
  }
}
</script>

<style scoped>
.player-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  box-shadow: var(--el-box-shadow);
  border-radius: 15px;
}

.clickable {
  margin-left: 20px;
  margin-right: 20px;
  color: #909399;
  transition: 0.3s ease;
}
.clickable:hover {
  cursor: pointer;
  color: var(--el-color-primary);
  transition: 0.3s ease;
}
</style>
